<template>
  <div class="sales-record">
    <el-card>
      <!-- 搜索栏 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="订单编号">
          <el-input v-model="searchForm.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="药品名称">
          <el-input v-model="searchForm.medicineName" placeholder="请输入药品名称" />
        </el-form-item>
        <el-form-item label="日期范围">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 统计卡片 -->
      <div class="statistics">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-card shadow="hover">
              <template #header>
                <div class="stat-header">
                  <span>今日销售额</span>
                  <el-tag type="success">+15%</el-tag>
                </div>
              </template>
              <div class="stat-content">
                <h2>¥{{ todayAmount }}</h2>
                <p>昨日：¥{{ yesterdayAmount }}</p>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover">
              <template #header>
                <div class="stat-header">
                  <span>本月销售额</span>
                  <el-tag type="warning">+8%</el-tag>
                </div>
              </template>
              <div class="stat-content">
                <h2>¥{{ monthAmount }}</h2>
                <p>上月：¥{{ lastMonthAmount }}</p>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover">
              <template #header>
                <div class="stat-header">
                  <span>今日订单数</span>
                  <el-tag type="success">+20%</el-tag>
                </div>
              </template>
              <div class="stat-content">
                <h2>{{ todayOrders }}</h2>
                <p>昨日：{{ yesterdayOrders }}</p>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover">
              <template #header>
                <div class="stat-header">
                  <span>本月订单数</span>
                  <el-tag type="primary">+12%</el-tag>
                </div>
              </template>
              <div class="stat-content">
                <h2>{{ monthOrders }}</h2>
                <p>上月：{{ lastMonthOrders }}</p>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 数据表格 -->
      <el-table
        v-loading="loading"
        :data="tableData"
      >
        <el-table-column prop="orderNo" label="订单编号" width="180" />
        <el-table-column prop="medicineName" label="药品名称" width="180" />
        <el-table-column prop="quantity" label="数量" width="100" />
        <el-table-column prop="price" label="单价" width="120">
          <template #default="{ row }">
            ¥{{ row.price }}
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="金额" width="120">
          <template #default="{ row }">
            ¥{{ row.amount }}
          </template>
        </el-table-column>
        <el-table-column prop="customerName" label="客户名称" width="120" />
        <el-table-column prop="createTime" label="销售时间" width="180" />
        <el-table-column prop="operator" label="操作员" width="120" />
        <el-table-column prop="remark" label="备注" />
      </el-table>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'

// 搜索表单
const searchForm = reactive({
  orderNo: '',
  medicineName: '',
  dateRange: [] as string[]
})

// 统计数据
const todayAmount = ref(12580.00)
const yesterdayAmount = ref(10940.00)
const monthAmount = ref(286500.00)
const lastMonthAmount = ref(265000.00)
const todayOrders = ref(156)
const yesterdayOrders = ref(130)
const monthOrders = ref(3250)
const lastMonthOrders = ref(2900)

// 表格数据
const loading = ref(false)
const tableData = ref([
  {
    orderNo: 'SO001',
    medicineName: '阿莫西林胶囊',
    quantity: 2,
    price: 29.9,
    amount: 59.8,
    customerName: '张三',
    createTime: '2024-03-18 10:00:00',
    operator: '王五',
    remark: '普通销售'
  }
])

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 搜索
const handleSearch = () => {
  // TODO: 实现搜索逻辑
  console.log('search:', searchForm)
}

// 重置搜索
const resetSearch = () => {
  searchForm.orderNo = ''
  searchForm.medicineName = ''
  searchForm.dateRange = []
  handleSearch()
}

// 分页大小变化
const handleSizeChange = (val: number) => {
  pageSize.value = val
  handleSearch()
}

// 页码变化
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  handleSearch()
}
</script>

<style scoped>
.sales-record {
  padding: 20px;
}
.search-form {
  margin-bottom: 20px;
}
.statistics {
  margin-bottom: 20px;
}
.stat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.stat-content {
  text-align: center;
}
.stat-content h2 {
  margin: 10px 0;
  font-size: 24px;
  color: #303133;
}
.stat-content p {
  margin: 0;
  font-size: 14px;
  color: #909399;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 